package com.lx.zxs.controller;

import com.lx.zxs.entity.Contacts;
import com.lx.zxs.entity.Admin;
import com.lx.zxs.entity.User;
import com.lx.zxs.service.AdminService;
import com.lx.zxs.service.ContactsService;
import com.lx.zxs.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@CrossOrigin
@RestController
@RequestMapping("/contacts")
public class ContactsController {

    @Autowired
    private ContactsService contactsService;

    @Autowired
    private UserService userService;

    @Autowired
    private AdminService adminService;


    /**
     * 账号密码验证
     *
     * @param username
     * @param pass
     * @param res
     * @return
     */
    @RequestMapping(value = "/adminlogin")
    public Admin getAdminById(String username, String pass, HttpServletResponse res, HttpSession session) {
        Admin admin = adminService.getadminByNameAndPass(username,pass);
        session.setAttribute("admin", admin);
        return admin;
    }


    /**
     * 根据条件查询消息
     *
     * @param keyWords
     * @param uid
     * @return
     */
    @RequestMapping("getContactsAll")
    public List<Contacts> getContactsByParms(String keyWords, Integer id) {

        return contactsService.getContactsByParms(keyWords, id);
    }


    /**
     * 添加消息
     *
     * @return
     */
    @RequestMapping("insertConcats")
    public int insertConcats(int uid, String ctime, String linkname, String content) {

        Contacts contacts = new Contacts();
        contacts.setUid(uid);
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            //使用SimpleDateFormat的parse()方法生成Date
            Date date = sf.parse(ctime);
            contacts.setCtime(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        contacts.setLinkname(linkname);
        contacts.setContent(content);
//        contacts.setUuid(UUID.randomUUID().toString().replaceAll("-",""));

        return this.contactsService.insertConcats(contacts);
    }

    /**
     * 修改消息
     * @return
     */
    @RequestMapping("updateContacts")
    public int updateContacts(int id,int uid,String ctime,String linkname,String content){

        Contacts contacts = new Contacts();
        contacts.setUid(uid);
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            //使用SimpleDateFormat的parse()方法生成Date
            Date date = sf.parse(ctime);
            contacts.setCtime(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        contacts.setLinkname(linkname);
        contacts.setContent(content);
        contacts.setId(id);
//        contacts.setUuid(UUID.randomUUID().toString().replaceAll("-",""));

        return this.contactsService.updateConcats(contacts);

    }

    /**
     * 删除消息
     * @param id
     * @return
     */
    @RequestMapping("delContacts")
    public int delContacts(int id){

        return contactsService.delContacts(id);
    };

}